package com.boot.security.server.controller.system;

import com.boot.security.server.dao.mffl.MffCashwithdrawalapplicationDao;
import com.boot.security.server.dao.mffl.MfflUserDao;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.MffCashwithdrawalapplication;
import com.boot.security.server.model.mffl.MfflUser;
import com.boot.security.server.model.mffl.Vo.MffCashwithdrawalapplicationVo;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.ProjectUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/mffCashwithdrawalapplications")
public class MffCashwithdrawalapplicationController {

    @Autowired
    private MffCashwithdrawalapplicationDao mffCashwithdrawalapplicationDao;

    @Autowired
    private MfflUserDao mfflUserDao;

    @PostMapping
    @ApiOperation(value = "保存")
    @PreAuthorize("hasAnyAuthority('sys:Cashwithdrawalapplications:add')")
    public MffCashwithdrawalapplication save(@RequestBody MffCashwithdrawalapplication mffCashwithdrawalapplication) {
        mffCashwithdrawalapplicationDao.save(mffCashwithdrawalapplication);

        return mffCashwithdrawalapplication;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    @PreAuthorize("hasAnyAuthority('sys:Cashwithdrawalapplications:query')")
    public MffCashwithdrawalapplicationVo get(@PathVariable Long id) {
        return mffCashwithdrawalapplicationDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @PreAuthorize("hasAnyAuthority('sys:Cashwithdrawalapplications:add')")
    public MffCashwithdrawalapplication update(@RequestBody MffCashwithdrawalapplication mffCashwithdrawalapplication) {
        mffCashwithdrawalapplicationDao.update(mffCashwithdrawalapplication);

        return mffCashwithdrawalapplication;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    @PreAuthorize("hasAnyAuthority('sys:Cashwithdrawalapplications:query')")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mffCashwithdrawalapplicationDao.countSystem(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MffCashwithdrawalapplicationVo> list(PageTableRequest request) {
                return mffCashwithdrawalapplicationDao.listSystem(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        mffCashwithdrawalapplicationDao.delete(id);
    }

    @PostMapping("/finish/{id}")
    @ApiOperation(value = "同意提现")
    @Transactional
    public ReturnBean finish(@PathVariable Long id) {

        SysUser sysUser = UserUtil.getLoginUser();

        int status = this.mffCashwithdrawalapplicationDao.updateState(1, id, sysUser.getId());
        if (status == 0) {
            return new ReturnBean("201", "处理失败");
        }
        //删除资产
        MffCashwithdrawalapplication mffCashwithdrawalapplication = this.mffCashwithdrawalapplicationDao.getById(id);
        MfflUser mfflUser = this.mfflUserDao.getUserInfoAll(mffCashwithdrawalapplication.getUserid());
        this.mfflUserDao.updateMoney(ProjectUtil.subtract(mfflUser.getMoney(), mffCashwithdrawalapplication.getExpenditure()),
                ProjectUtil.subtract(mfflUser.getFrozenmoney(), mffCashwithdrawalapplication.getExpenditure()), mffCashwithdrawalapplication.getUserid());
        return new ReturnBean();
    }

    @PostMapping("/refuse/{id}")
    @ApiOperation(value = "拒绝提现")
    @Transactional
    public ReturnBean refuse(@PathVariable Long id) {
        SysUser sysUser = UserUtil.getLoginUser();
        int status = this.mffCashwithdrawalapplicationDao.updateState(2, id, sysUser.getId());
        if (status == 0) {
            return new ReturnBean("201", "处理失败");
        }
        //删除资产
        MffCashwithdrawalapplication mffCashwithdrawalapplication = this.mffCashwithdrawalapplicationDao.getById(id);
        MfflUser mfflUser = this.mfflUserDao.getUserInfoAll(mffCashwithdrawalapplication.getUserid());
        this.mfflUserDao.updateMoney(new BigDecimal(mfflUser.getMoney()),
                ProjectUtil.subtract(mfflUser.getFrozenmoney(), mffCashwithdrawalapplication.getExpenditure()), mffCashwithdrawalapplication.getUserid());
        return new ReturnBean();
    }
}
